#ifndef __VIDEOENCODER_H__
#define __VIDEOENCODER_H__

#include "video_capture.h"
#include "ESVideo.h"
#include "MyThread.h"
#include "ListPtr.h"
#include "Lock.h"

typedef struct tagEncoderParam
{
	int		nWidth;
	int		nHeight;
	int		nFrame;
	int		nBitRate;
	int		nQMin;
	int		nQMax;
	int		nQuant;
	int		nThreadNum;
	tagEncoderParam()
	{
		nWidth = 320;
		nHeight = 240;
		nFrame = 15;
		nBitRate = 1024;
		nQMin = 1;
		nQMax = 51;
		nQuant = 32;
		nThreadNum = 0;
	}
}EncoderParam,*LPEncoderParam;

typedef struct tagH264VideoParam
{
	char			*pBuf;
	int				nDataLen;
	int				nIsKeyFrame;
	int				nWidth;
	int				nHeight;
	int				nFrameRate;
	unsigned long	ulTimestamp;
	tagH264VideoParam()
	{
		pBuf = NULL;
		nIsKeyFrame = 0;
	}
}H264VideoParam;

struct YUVDataBuff
{
	char *pData;
	int nDataLen;
	int		nWidth;
	int		nHeigth;
	unsigned long ulTimestamp;
};

class EncodeDataNotify
{
public:
	EncodeDataNotify(void){};
	virtual~EncodeDataNotify(void){};
public:
	virtual void OnEncodeData(H264VideoParam *videoData) = 0;
};

class CVideoEncoder : public CMyThread
{
public:
	CVideoEncoder(EncodeDataNotify *pNotify);
	~CVideoEncoder(void);

	bool OpenEncoder(const EncoderParam &encoderParam);
	void PutYUVData(const uint8_t * const pFrame[], int nWidth, int nHeight, unsigned long ulTimestamp);
	
protected:
	virtual void ThreadProcMain(void);

private:	
	void CloseEncoder(void);
	void ClearRGBBuf(YUVDataBuff *pYUV);

	int Encode(YUVDataBuff *pYUV);

	YUVDataBuff *GetYuvBuff(void);
private:
	void				*m_hEncodeHandle;
	EncodeDataNotify	*m_pNotify;

	EncoderParam		m_param;
	bool				m_bExit;

	ListPtr				m_lstYUVData;
	CLock				m_lockYUVData;

	ListPtr				m_lstFree;
	int					m_nBuffCount;
	
	char				*m_pH264Buf;
	int					m_nBufLen;

	int					m_nSrcWidth;
	int					m_nSrcHeight;

	FILE				*m_pOutFile;
};
#endif
